/*--------------------------------------------------------------
user_tcp_s_axis_tuser:
[67:66]---0:udp  1:tcp
[65:64]---当前处于tcp模式，0:发送数据，1:打开tcp链接，2：关闭连接
[63:48]---tx_target_port
[47:16]---tx_target_ip
[15:0]---tx_data_len

user_tcp_m_axis_tuser:
[63:48]---rx_source_port
[47:16]---rx_source_ip
[15:0]---rx_data_len

tcp_s_axis_tuser:
[55:24]---rx_ip_source_ip
[23:16]---rx_pkg_type
[15:0]---rx_ip_data_len

tcp_m_axis_tuser:
[55:24]---tx_ip_target_ip
[23:16]---tx_pkg_type  udp=17
[15:0]---tx_ip_data_len
--------------------------------------------------------------*/

module ethernet_tcp_top #(
    parameter   SOURCE_PORT         = 16'd5001                          ,
    parameter   SOURCE_IP           = {8'd192,8'd192,8'd7,8'd11    }        
)(
    input                           i_clk                               ,
    input                           i_rst                               ,
    input                           i_set_source_port_valid             ,
    input   [15: 0]                 i_set_source_port                   ,
    input                           i_set_source_ip_valid               ,
    input   [31: 0]                 i_set_source_ip                     ,
    output                          user_tcp_m_axis_tvalid              ,
    output  [7 : 0]                 user_tcp_m_axis_tdata               ,
    output                          user_tcp_m_axis_tlast               ,
    output  [63: 0]                 user_tcp_m_axis_tuser               ,  
    input                           user_tcp_s_axis_tvalid              ,
    input   [7 : 0]                 user_tcp_s_axis_tdata               ,
    output                          user_tcp_s_axis_tready              ,
    input                           user_tcp_s_axis_tlast               ,
    input   [67: 0]                 user_tcp_s_axis_tuser               ,
    input                           tcp_s_axis_tvalid                   ,
    input   [7 : 0]                 tcp_s_axis_tdata                    ,
    input                           tcp_s_axis_tlast                    ,
    input   [71: 0]                 tcp_s_axis_tuser                    , 
    output                          tcp_m_axis_tvalid                   ,
    output  [7 : 0]                 tcp_m_axis_tdata                    ,
    input                           tcp_m_axis_tready                   ,
    output                          tcp_m_axis_tlast                    ,
    output  [55: 0]                 tcp_m_axis_tuser                    
);

    logic                           tcp_rx_valid                        ;
    logic   [31: 0]                 tcp_rx_id                           ;
    logic   [31: 0]                 tcp_rx_ack_id                       ;
    logic   [5 : 0]                 tcp_rx_flags                        ;
    logic   [15: 0]                 tcp_rx_windows_size                 ;
    logic                           flag_500ms                          ;
    logic                           flag_250ms                          ;
    

    tcp_timer tcp_timer_inst(
        .i_clk                          ( i_clk                     ),      
        .i_rst                          ( i_rst                     ),
        .o_flag_500ms                   ( flag_500ms                ),
        .o_flag_250ms                   ( flag_250ms                )
    );


    tcp_rx  #(
        .SOURCE_PORT                    ( SOURCE_PORT               )
    ) tcp_rx_inst(
        .i_clk                          ( i_clk                     ),
        .i_rst                          ( i_rst                     ),
        .i_set_source_port_valid        ( i_set_source_port_valid   ),
        .i_set_source_port              ( i_set_source_port         ),
        .s_axis_tvalid                  ( tcp_s_axis_tvalid         ),
        .s_axis_tdata                   ( tcp_s_axis_tdata          ),
        .s_axis_tlast                   ( tcp_s_axis_tlast          ),
        .s_axis_tuser                   ( tcp_s_axis_tuser          ),
        .m_axis_tvalid                  ( user_tcp_m_axis_tvalid    ),
        .m_axis_tdata                   ( user_tcp_m_axis_tdata     ),
        .m_axis_tlast                   ( user_tcp_m_axis_tlast     ),
        .m_axis_tuser                   ( user_tcp_m_axis_tuser     ),
        .o_tcp_rx_valid                 ( tcp_rx_valid              ), 
        .o_tcp_rx_id                    ( tcp_rx_id                 ),
        .o_tcp_rx_ack_id                ( tcp_rx_ack_id             ),
        .o_tcp_rx_flags                 ( tcp_rx_flags              ),
        .o_tcp_rx_windows_size          ( tcp_rx_windows_size       ) 
    );

    tcp_tx #(
        .SOURCE_PORT                    ( SOURCE_PORT               ),
        .SOURCE_IP                      ( SOURCE_IP                 )     
    )tcp_tx_inst(
        .i_clk                          ( i_clk                     ),
        .i_rst                          ( i_rst                     ),
        .i_set_source_port_valid        ( i_set_source_port_valid   ),
        .i_set_source_port              ( i_set_source_port         ),
        .i_set_source_ip_valid          ( i_set_source_ip_valid     ),
        .i_set_source_ip                ( i_set_source_ip           ),
        .i_timer_250ms                  ( flag_250ms                ),
        .i_timer_500ms                  ( flag_500ms                ),
        .i_tcp_rx_valid                 ( tcp_rx_valid              ), 
        .i_tcp_rx_id                    ( tcp_rx_id                 ),
        .i_tcp_rx_ack_id                ( tcp_rx_ack_id             ),
        .i_tcp_rx_flags                 ( tcp_rx_flags              ),
        .i_tcp_rx_windows_size          ( tcp_rx_windows_size       ), 
        .s_axis_tvalid                  ( user_tcp_s_axis_tvalid    ),
        .s_axis_tdata                   ( user_tcp_s_axis_tdata     ),
        .s_axis_tready                  ( user_tcp_s_axis_tready    ),
        .s_axis_tlast                   ( user_tcp_s_axis_tlast     ),
        .s_axis_tuser                   ( user_tcp_s_axis_tuser     ),
        .m_axis_tvalid                  ( tcp_m_axis_tvalid         ),
        .m_axis_tdata                   ( tcp_m_axis_tdata          ),
        .m_axis_tready                  ( tcp_m_axis_tready         ),
        .m_axis_tlast                   ( tcp_m_axis_tlast          ),
        .m_axis_tuser                   ( tcp_m_axis_tuser          )  
);




endmodule